package cn.felord.message.controller;

import cn.felord.message.bean.ResponseEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * rabbitmq消息接口.
 *
 * @author tanqian
 * @version v1.0
 * @since 2019 /8/2
 */
@RestController
@RequestMapping("/rabbit")
public class SendController {
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 测试Direct模式.
     *
     * @param p the p
     * @return the response entity
     */
    @RequestMapping("/direct")
    public ResponseEntity direct(String p) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend("HAND_DIRECT_EXCHANGE", "yida.yuntu.huiyun", p, correlationData);
        return ResponseEntity.ok();
    }

    /**
     * 测试广播fanout模式.
     *
     * @param p the p
     * @return the response entity
     */
    @RequestMapping("/fanout")
    public ResponseEntity fanout(String p) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend("HAND_FANOUT_EXCHANGE", "", p, correlationData);
        return ResponseEntity.ok();
    }

    /**
     * 测试Topic模式.
     * @param p
     * @param routingKey
     * @return
     */
    @RequestMapping("/topic")
    public ResponseEntity topic(String p, String routingKey) {

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("HAND_TOPIC_EXCHANGE", routingKey, p, correlationData);

        return ResponseEntity.ok();
    }


    /**
     * 测试Headers模式.
     * @param p
     * @return
     */
    @RequestMapping("/headers")
    public ResponseEntity headers(String p) {

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        Map<String, Object> map_all = new HashMap<>();
        map_all.put("One", "A");

        /**
         * 声明消息 (消息体, 消息属性)
         */
        MessageProperties messageProperties = new MessageProperties();
        // 设置消息是否持久化。Persistent表示持久化，Non-persistent表示不持久化
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        messageProperties.setContentType("UTF-8");
        messageProperties.getHeaders().putAll(map_all);

        Message message = new Message(p.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend("HAND_HEADERS_EXCHANGE", null, message, correlationData);

        return ResponseEntity.ok();
    }

    /**
     * 测试死信队列.
     *
     * @param p the p
     * @return the response entity
     */
    @RequestMapping("/dead")
    public ResponseEntity deadLetter(String p) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        声明消息处理器  这个对消息进行处理  可以设置一些参数   对消息进行一些定制化处理   我们这里  来设置消息的编码  以及消息的过期时间  因为在.net 以及其他版本过期时间不一致   这里的时间毫秒值 为字符串
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
//            设置编码
            messageProperties.setContentEncoding("utf-8");
//            设置过期时间10*1000毫秒
            messageProperties.setExpiration("10000");
            return message;
        };
//         向DL_QUEUE 发送消息  10*1000毫秒后过期 形成死信
        rabbitTemplate.convertAndSend("HAND_DL_EXCHANGE", "yida.hand", p, messagePostProcessor, correlationData);
        return ResponseEntity.ok();
    }
}
